package com.cskaoyan.controller.mall;

import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.po.MarketCategory;
import com.cskaoyan.bean.vo.mall.CategoryLabelValueVo;
import com.cskaoyan.service.mall.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("admin/category")
public class CategoryController {

    @Autowired
    CategoryService categoryService;

    @RequestMapping("list")
    public BaseRespVo queryCategoryList() {
        CommonData<MarketCategory> commonData = categoryService.queryCategoryList();
        return BaseRespVo.succeed(commonData);
    }

    @RequestMapping("l1")
    public BaseRespVo queryLabelValue() {
        CommonData<CategoryLabelValueVo> commonData = categoryService.queryLabelValue();
        return BaseRespVo.succeed(commonData);
    }

    @RequestMapping("create")
    public BaseRespVo createCategory(@RequestBody MarketCategory marketCategory) {
        MarketCategory marketCategory1 = categoryService.createCategory(marketCategory);
        if (marketCategory1 == null) {
            return BaseRespVo.invalidParameter("添加失败！请重试！");
        }
        return BaseRespVo.succeed(marketCategory1);
    }

    @RequestMapping("delete")
    public BaseRespVo deleteCategory(@RequestBody MarketCategory marketCategory) {
        int code = categoryService.deleteCategory(marketCategory.getId());
        if (code == 0) {
            return BaseRespVo.invalidParameter("删除失败！请重试！");
        }
        return BaseRespVo.succeed();
    }

    @RequestMapping("update")
    public BaseRespVo updateCategory(@RequestBody MarketCategory marketCategory) {
        int code = categoryService.updateCategory(marketCategory);
        if (code == 0) {
            return BaseRespVo.invalidParameter("修改失败！请重试！");
        }
        return BaseRespVo.succeed();
    }

}
